const express = require('express')
const cookieSession = require('cookie-session')
const path = require('path')
const fs = require('fs')
const app = express()
app.listen(3000)

// 通过中间件完成引入
app.use(cookieSession({
  name: 'sessionid',
  // secret: 'fefewjfewklfjlwfjewlfewlfjewlfjewlfewlfjwfjwlfwlfwfwe',
  secret: fs.readFileSync(path.join(__dirname, '.secret')),
  // maxAge: 3600 * 1000
}))

// session只能在服务器端设置，js不能获取得
app.get('/login', (req, res) => {

  // 设置session
  req.session['username'] = 'admin'


  // res.send('aaaaaa')
  res.redirect('/user')
})

app.get('/user', (req, res) => {
  // 获取session
  console.log(req.session['username']);

  res.send('111')
})
